package com.xuelang.pipeline.artifact.app.harborv2.fallback;

import com.xuelang.pipeline.artifact.app.harborv2.MemberService;
import com.xuelang.pipeline.artifact.app.harborv2.request.CreateMemberRequest;
import com.xuelang.pipeline.artifact.app.harborv2.request.ListMemberQuery;
import com.xuelang.pipeline.artifact.app.harborv2.response.SearchUserResponse;
import com.xuelang.pipeline.artifact.app.nexus3v1.exception.ArtifactException;
import com.xuelang.pipeline.artifact.app.nexus3v1.xo.ApiResponseXO;
import org.springframework.cloud.openfeign.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author xiwen.zxg
 * @since 2024/7/10
 */
@Slf4j
@Service
public class MemberServiceFallbackFactory implements FallbackFactory<MemberService> {
    @Override
    public MemberService create(Throwable throwable) {
        ApiResponseXO<Void> apiResponseXO = new ApiResponseXO<>();
        if (throwable instanceof ArtifactException) {
            ArtifactException artifactException = (ArtifactException) throwable;
            apiResponseXO = artifactException.getApiResponse();
        }
        final ApiResponseXO resultApiResponseXO = ApiResponseXO.builder()
                .status(apiResponseXO.getStatus())
                .id(apiResponseXO.getId())
                .message(apiResponseXO.getMessage())
                .data(apiResponseXO.getData())
                .build();
        log.error("{} happened to exception with {}", this.getClass().getName(), throwable.getMessage());
        return new MemberService() {

            @Override
            public ApiResponseXO<Void> listProjectMember(String projectNameOrId, ListMemberQuery query) {
                return resultApiResponseXO;
            }

            @Override
            public ApiResponseXO<SearchUserResponse> createProjectMember(String projectNameOrId, CreateMemberRequest request) {
                return resultApiResponseXO;
            }

            @Override
            public ApiResponseXO<Void> deleteProjectMember(String projectNameOrId, String memberId) {
                return resultApiResponseXO;
            }
        };
    }
}
